% This function is to compute the CBF and AAT by CMF
% for different TIs and different Repetitions, Then save all results in a
% /Results/ directory.
%% Read all asl data into memory
close all
clear all
parameters = divasl_set_parameter('default');
[f_list_com0,f_list_coasl,f_list_mri] = divasl_get_filelist(parameters);

% obj_divasl = cDIVASL(f_list_com0,f_list_coasl,f_list_mri);
obj_divasl = cDIVASL(f_list_mri);



%% Read the template images
% templates should be resliced before using
%  job_reslice_file(true,'',false,'c2rT13D_asl_file.nii','cpwall_nrtasl_file_gm.nii');

f_template_graymatter = 'myProject/template/rc1rT13D_asl_file_on_cpwall_nrtasl_file_gm_nii.nii';
f_template_whitematter = 'myProject/template/rc2rT13D_asl_file_on_cpwall_nrtasl_file_gm_nii.nii';
f_template_shell = 'myProject/template/rc3rT13D_asl_file_on_cpwall_nrtasl_file_gm_nii.nii';

obj_divasl = obj_divasl.setTemplate(...
    'gray',f_template_graymatter,...
    'white',f_template_whitematter,...
    'shell',f_template_shell);

%% Compute different number of TIs and repetitions
nIter = 100;
results(nIter) = cPerfusion;
for nTi = 10:11
    for nRep = 5:1:40
        if ~mod(nRep,5)
            continue;
        end
        for n = 1:nIter
            tic;
            
            idxTi = randperm(11);idxTi = idxTi(1:nTi);
            idxRep = randperm(40);idxRep = idxRep(1:nRep);
            obj_divasl = obj_divasl.callCMF('Gray',['[',num2str(idxRep),']'],idxTi);
            
            
            results(n) = obj_divasl.dCMF;
            %             obj_divasl.dCMF.saveResults('Results/',fileLog);
            %             disp(fileLog);
            time_use = toc;
            disp(['Time estimated: ',num2str(time_use*nIter*length(nRep:1:40)*length(nTi:11)/3600),' hours']);
        end
        fileLog = [];
        results.saveResults('Results/DiverseTIREP_100/',fileLog);
        disp(fileLog);
        
        
    end
end


%
% obj_divasl = obj_divasl.callCMF('Gray','1:30',1:2:11);
%
% obj_divasl.dCMF.showResult('cbf',obj_divasl);
% obj_divasl.dCMF.showResult('aat',obj_divasl);

